package com.zb.spring5.enums;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public enum Operation {
    PLUS("+") { @Override
    public double apply(double x, double y) { return x + y; } },
    MINUS("-") { @Override
    public double apply(double x, double y) { return x - y; } },
    TIMES("*") { @Override
    public double apply(double x, double y) { return x * y; } },
    DIVIDE("/") { @Override
    public double apply(double x, double y) { return x / y; } };
    private final String symbol; Operation(String symbol) { this.symbol = symbol; }
    @Override
    public String toString() { return symbol; }
    public abstract double apply(double x, double y);

    private static final Map<String,Operation> stringToEnum = Stream.of(values()).collect(
            Collectors.toMap(Objects::toString,e->e));
    public static Optional<Operation> fromString(String symbol){
        return Optional.ofNullable(stringToEnum.get(symbol));
    }
}
